package br.mikhas.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class IOUtils {
	private static final String CLASSPATH = "classpath:";

	public static String read(String file) {
		if (file.startsWith(CLASSPATH)) {
			return readClasspath(file.substring(CLASSPATH.length()));
		}
		return null;
	}

	private static String readClasspath(String file) {
		ClassLoader classLoader = IOUtils.class.getClassLoader();
		InputStream inputStream = classLoader.getResourceAsStream(file);
		return read(inputStream);
	}

	public static String read(InputStream input) {
		Reader reader = new InputStreamReader(input);
		StringBuilder builder = new StringBuilder();

		int size = 1024;
		int count;
		char[] buff = new char[size];
		try {
			while ((count = reader.read(buff)) > 0) {
				builder.append(buff, 0, count);
			}
			return builder.toString();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}
